package server;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import server.HttpServlet;
import server.MyClassLoader;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 解析server.xml 一个应用相关信息
 */
public class Context {
    //存放的路径  appBase = "F:\studyProject\Minicat\webapps"
    private String appBase;
    //表示访问的uri的前缀路径 webapps 下的应用文件名 demo1 demo2
    private String path;
    
    private ClassLoader classLoader;

    private Map<String,HttpServlet> servletMap = new HashMap<String,HttpServlet>();

    public Context(String appBase, String path) {
        this.appBase = appBase;
        this.path = path;
        //设置当前应用的类加载器
        MyClassLoader myClassLoader = new MyClassLoader(appBase, path);
        classLoader = myClassLoader;
        //解析当前项目包下的web.xml
        loadWebServlet();
    }

    private void loadWebServlet() {
        Map<String,HttpServlet> servletMap1 = new HashMap<String,HttpServlet>();
        FileInputStream resourceAsStream = null;
        try {
            resourceAsStream = new FileInputStream(appBase + "//" + path + "/web.xml");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(resourceAsStream);
            Element rootElement = document.getRootElement();

            List<Element> selectNodes = rootElement.selectNodes("//servlet");
            for (int i = 0; i < selectNodes.size(); i++) {
                Element element =  selectNodes.get(i);
                // <servlet-name>lagou</servlet-name>
                Element servletnameElement = (Element) element.selectSingleNode("servlet-name");
                String servletName = servletnameElement.getStringValue();
                // <servlet-class>server.LagouServlet</servlet-class>
                Element servletclassElement = (Element) element.selectSingleNode("servlet-class");
                String servletClass = servletclassElement.getStringValue();


                // 根据servlet-name的值找到url-pattern
                Element servletMapping = (Element) rootElement.selectSingleNode("/web-app/servlet-mapping[servlet-name='" + servletName + "']");
                // /lagou
                String urlPattern = servletMapping.selectSingleNode("url-pattern").getStringValue();

                HttpServlet instance =(HttpServlet)classLoader.loadClass(servletClass).newInstance();

                servletMap.put("/" +path + urlPattern, instance);
                servletMap1.put(urlPattern, instance);

            }



        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
 
    }

    public String getAppBase() {
        return appBase;
    }

    public void setAppBase(String appBase) {
        this.appBase = appBase;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public ClassLoader getClassLoader() {
        return classLoader;
    }

    public void setClassLoader(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    public Map<String, HttpServlet> getServletMap() {
        return servletMap;
    }

    public void setServletMap(Map<String, HttpServlet> servletMap) {
        this.servletMap = servletMap;
    }
}
